<template>
  <div>
    <!-- 提示信息 -->
    <div class="tipinfo" v-show="isTip">{{tipword}}</div>
    <input type="text" name="account123" style="display:none;" auto-complete="off"/>
    <input type="password" name="password123" style="display:none;" auto-complete="off"/>
    <!-- 验证部分 -->
    <div class="mask" v-show="yzphone">
      <div class="box">
        <div class="wenku hand" @click="closeWindow">验证手机</div>
        <div class="wenkudec">请输入您的手机号和6位短信验证码</div>

        <div class="gap70"></div>
        <div class="inputbox">
          <input type="text" class="inputtext" v-model="phone_zc" placeholder="请输入手机号">
        </div>
        <div class="inputbox">

          <input type="text" class="inputtext" v-model="yzcode" placeholder="请输入验证码">
          <div class="yanzheng hand" @click="sendCode()" v-show="sendAuthCode">发送验证码</div>
          <div class="yanzheng hand" v-show="!sendAuthCode" > <span class="auth_text_blue">{{auth_time}} </span> 秒后重新发送</div>      </div>
        <div class="wkbtn" @click="login()">确认绑定</div>
      </div>
    </div>

    <div class="mask" v-show="isSetPassword">
      <div class="forgetbox" >
        <div class="close" @click="closeWindow()"></div>
        <div class="fftitle">设置密码</div>
        <div class="ffbox">
          <input type="password" class="ffinputtext" v-model="newpass" maxlength="10" placeholder="请输入密码（8位以上数字和字母）">
        </div>
        <div class="ffbox">
          <input type="password" class="ffinputtext" v-model="newpassagain" maxlength="10" placeholder="再次输入密码">
        </div>
        <div class="ffnextbtn hand" @click="goSetPass1">提交</div>
      </div>
    </div>
  </div>
</template>

<script>
  import axios  from 'axios'
  import qs from 'qs'
    export default {
        name: "wb",
        data(){
          return{
            code:'',
            state:'',
            yzphone:false,
            phone_zc:'',
            yzcode:'',
            sendAuthCode:true,
            auth_time:0,
            // 提示
            isTip:false,
            tipword:'',
            usertmp:'',
            isSetPassword:'',
            newpass:'',
            newpassagain:'',
            showinfo:''
          }

        },created(){

      },
      mounted(){
        this.code=this.$route.query.code;
        this.state=this.$route.query.state;
        console.log(this.code+" "+this.state);
        if(this.code!=null && this.code!="" && this.code!=undefined && this.state!=null && this.state!="" && this.state!=undefined){
          this.getWbUserInfo();
        }else{

        }
      },
      methods:{
        getSessionUser(){

          let key=localStorage.getItem("user_unique_code");
          if(key!=null && key!="" && key!=undefined){
            //用户可能是登陆状态
            console.log("key "+key)
            axios.post('https://wenku.qikan.com:11000/admin/lib/getTokenByUserId.html',qs.stringify({'key':key})
            )
              .then((response)=>{
                console.log("resultuser "+response.data);
                let result= response.data;
                console.log("ccode "+response.data.code)
                if(result.code==1){//用户是登陆状态，
                  console.log(result.data.trim())
                  this.user=JSON.parse(result.data.trim());
                  console.info("usermsg "+this.user)
                  // if(this.user!=null && this.user!="" && this.user!=undefined){
                  //   // this.poster=this.user.poster;
                  //   // console.log("uuaa  "+this.user.mobile)
                  //   // this.isLogin=true;
                  // }\
                  this.getWbUserInfo();
                }else{//用户是未登陆状态，解绑
                  axios.post('https://wenku.qikan.com:11000/admin/lib/getUserByUniqueCode.html',qs.stringify({'unique_code':key})
                  )
                    .then((response)=>{
                      console.log(response.data);
                      let result= response.data;
                      if(result.code==1){
                        //获取用户信息成功
                        localStorage.removeItem("user_unique_code");
                        localStorage.setItem("user_unique_code",result.data.unique_code);
                        console.log("用户信息lalala "+result.data);
                        this.user =result.data;
                        let v=JSON.stringify(this.user);
                        console.log(this.user)
                        axios.post('https://wenku.qikan.com:11000/admin/lib/removeTokenByUserId.html',qs.stringify({'key':result.data.unique_code}))
                          .then(function(res){

                            console.log(res.data.data);
                          })
                        axios.post('https://wenku.qikan.com:11000/admin/lib/setTokenByUserId.html',qs.stringify({'key':result.data.unique_code,'value':v}))
                          .then(function(res){
                            console.log(res.data.data);
                          })
                        this.getWbUserInfo();
                      }else{
                        this.tipshow("用户已过期，请重新登陆！")
                        this.$router.push({path: '/'});
                      }
                    })
                    .catch((error)=>{
                      console.log(error)
                    })
                }
              })
              .catch((error)=>{
                console.log(error)
              })
          }else{//用户是未登陆状态
            this.getWbUserInfo();
          }
        },
          goSetPass1(){
          // let regExp=/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,21}$/;
          // let regu = "^[0-9a-zA-Z]{8,10}$";
          // let regExp = new RegExp(regu);
          let regExp = new RegExp(/^(?![^a-zA-Z]+$)(?!\D+$)/);

          console.log("info")
          if(this.newpass==null || this.newpass=="" || this.newpass==undefined){
            console.log("info1")

            this.tipshow("新密码不能为空！");
            return false;
          }else if(this.newpassagain==null || this.newpassagain=="" || this.newpassagain==undefined){
            console.log("info2")

            this.tipshow("请再次输入密码！");
            return false;
          }else if(this.newpass!=this.newpassagain){
            console.log("info3")

            this.tipshow("两次密码输入不一致！");
            return false;
          }else if(!regExp.test(this.newpass) ||!regExp.test(this.newpassagain)|| this.newpass.length<8 || this.newpass.length>10
            || this.newpassagain<8 || this.newpassagain>10){
            console.log("info4")

            this.tipshow("密码格式不正确！");
            return false;
          }else{

            console.log("validcode "+this.yzcode)
            let params={'mobile':this.phone_zc,'validcode':this.yzcode,'userType':3,'wbunionid':this.usertmp.idstr,'nick_name':this.usertmp.screen_name,'poster':this.usertmp.profile_image_url};
            console.log(params)
            axios.post('https://wenku.qikan.com:11000/admin/lib/userlogin.html',qs.stringify(params)
            )
              .then((response)=>{
                console.log("userlogin "+response.data);
                let result= response.data;
                if(result.code==1){
                  this.yzphone=false;
                  axios.post('https://wenku.qikan.com:11000/admin/lib/setPwd.html',qs.stringify({'mobile':this.phone_zc,'pwd':this.newpass,'pwdAgain':this.newpassagain})
                  )
                    .then((response)=>{
                      console.log("setpwd  "+response.data);
                      let result= response.data;
                      if(result.code==1){
                        this.isSetPassword=false;
                        this.phone_zc=''
                        this.yzcode=''
                        // localStorage.removeItem("user");
                        // localStorage.setItem("user", JSON.stringify(result.data));
                        localStorage.removeItem("user");
                        localStorage.setItem("user",JSON.stringify(result.data));
                        this.user =JSON.parse(localStorage.getItem("user"))
                        // let v=JSON.stringify(this.user);
                        // console.log(this.user)
                        // axios.post('https://wenku.qikan.com:11000/admin/lib/removeTokenByUserId.html',qs.stringify({'key':result.data.unique_code}))
                        //   .then(function(res){
                        //
                        //     console.log(res.data.data);
                        //   })
                        // axios.post('https://wenku.qikan.com:11000/admin/lib/setTokenByUserId.html',qs.stringify({'key':result.data.unique_code,'value':v}))
                        //   .then(function(res){
                        //     console.log(res.data.data);
                        //   })
                        // this.tipshow("设置密码成功，请登录！")
                        window.location.href="/person/setup";
                        // this.cancelRegister()
                        // localStorage.removeItem("user");
                        // localStorage.setItem("user", JSON.stringify(result.data));
                      }else{
                        this.tipshow("设置密码失败！")
                      }
                    })
                    .catch((error)=>{
                      console.log(error)
                    })

                  console.log('设置密码成功')
                  // localStorage.removeItem("user");
                  // localStorage.setItem("user", JSON.stringify(result.data));
                  // this.$router.push({path: '/'});
                }else{
                  this.tipshow(result.message);
                }
              })
              .catch((error)=>{
                console.log(error)
              })
            // console.log("info5")
            // let params={'mobile':this.phone_zc,'validcode':this.yzcode,'userType':1,'weixinunionid':this.usertmp.unionid,'wxopenid':this.usertmp.openid,'nick_name':this.usertmp.nickname,'poster':this.usertmp.headimgurl};
            // console.log(params)



          }


        },
        tipshow(e){
          this.tipword=e;
          this.isTip=true;
          setTimeout(() => {
            this.tipword='';
            this.isTip=false;
          }, 2000);
        },
        getWbUserInfo(){
          // let user=JSON.parse(localStorage.getItem("user"));
          // this.getSessionUser();
          let user=this.user;
          console.log("useeffff"+user);
          console.log(JSON.stringify(user))
          let params={};
          if(user!=null && user!="" && user!=undefined){
            params={'code':this.code,'state':this.state,'mobile':user.mobile};
          }else{
            params={'code':this.code,'state':this.state};
          }
          console.log(params);
          axios.post('https://wenku.qikan.com:11000/admin/lib/wbuserinfo.html',qs.stringify(params)
          )
            .then((response)=>{
              console.log("wb+ "+ response.data.code);
              let result= response.data;
              console.log("wv"+ result)
              if(user!=null && user!="" && user!=undefined){
                //zhuc，bangdinb
                console.log("bangding")
                localStorage.removeItem("user");
                localStorage.setItem("user",JSON.stringify(result.data));
                this.user =JSON.parse(localStorage.getItem("user"))
                // let v=JSON.stringify(this.user);
                // console.log(this.user)
                // axios.post('https://wenku.qikan.com:11000/admin/lib/removeTokenByUserId.html',qs.stringify({'key':result.data.unique_code}))
                //   .then(function(res){
                //
                //     console.log(res.data.data);
                //   })
                // axios.post('https://wenku.qikan.com:11000/admin/lib/setTokenByUserId.html',qs.stringify({'key':result.data.unique_code,'value':v}))
                //   .then(function(res){
                //     console.log(res.data.data);
                //   })
                //window.location.href="/person/setup"
                this.$router.push({path: '/person/setup'});

              }else{
                //注册，
                if(result.data.userstate!=null && result.data.userstate!="" && result.data.userstate!=undefined && result.data.userstate==1){
                  console.log("nobind");
                  console.log(this.sendAuthCode);
                  console.log(this.yzphone);

                  console.log(this.yzphone);
                  this.usertmp=result.data;
                  console.log("usertmp  "+this.usertmp)

                  if(result.data.pwd!=null && result.data.pwd!="" &&  result.data.pwd!=undefined ) {
                    this.showinfo="进入文库";
                  } else{
                    this.showinfo="设置密码";

                  }
                  this.yzphone=true;
                }else{
                  localStorage.removeItem("user");
                  localStorage.setItem("user",JSON.stringify(result.data));
                  this.user =JSON.parse(localStorage.getItem("user"))
                  // let v=JSON.stringify(this.user);
                  // console.log(this.user)
                  // axios.post('https://wenku.qikan.com:11000/admin/lib/removeTokenByUserId.html',qs.stringify({'key':result.data.unique_code}))
                  //   .then(function(res){
                  //
                  //     console.log(res.data.data);
                  //   })
                  // axios.post('https://wenku.qikan.com:11000/admin/lib/setTokenByUserId.html',qs.stringify({'key':result.data.unique_code,'value':v}))
                  //   .then(function(res){
                  //     console.log(res.data.data);
                  //   })
                  this.$router.push({path: '/'});

                }
              }
            })
            .catch((error)=>{
              console.log(error)
            })
        },tipshow(e){
          this.tipword=e;
          this.isTip=true;
          setTimeout(() => {
            this.tipword='';
            this.isTip=false;
          }, 2000);
        },closeWindow(){
          this.yzphone=false;
          this.$router.push({path: '/'});
        },login(){

          var mobilereg = /^1[345789]\d{9}$/;
          if (this.phone_zc == null || this.phone_zc == "" || this.phone_zc == 'undefined') {
            this.tipshow("手机号不能为空!");
            return false;
          } else if (!mobilereg.test(this.phone_zc)) {
            this.tipshow("手机格式不正确");
            return false;
          }else if(this.yzcode==null || this.yzcode =="" || this.yzcode == undefined || this.yzcode.length!=6){
            this.tipshow("验证码为空或长度不正确！");
            return false;
          }
          axios.post('https://wenku.qikan.com:11000/admin/lib/getUserByMobile.html',qs.stringify({'mobile':this.phone_zc})
          )
            .then((response)=>{
              console.log(response.data);
              let result= response.data;
              if(result.code==1){
                if(result.data.wbunionid!=null && result.data.wbunionid!="" && result.data.wbunionid!=undefined ){
                  this.tipshow("该手机号已经绑定微博！")
                  return false;
                }else{
                  if(result.data.pwd!=null && result.data.pwd!="" && result.data.pwd!=undefined){
                    console.log("validcode "+this.yzcode)
                    let params={'mobile':this.phone_zc,'validcode':this.yzcode,'userType':3,'wbunionid':this.usertmp.idstr,'nick_name':this.usertmp.screen_name,'poster':this.usertmp.profile_image_url};
                    console.log(params)

                    axios.post('https://wenku.qikan.com:11000/admin/lib/userlogin.html',qs.stringify(params)
                    )
                      .then((response)=>{
                        console.log(response.data);
                        let result= response.data;
                        console.log(result.code)
                        if(result.code==1){
                          console.log("info "+result.code)
                          // localStorage.removeItem("user_unique_code");
                          // localStorage.setItem("user_unique_code",result.data.unique_code);
                          // // localStorage.setItem("user", JSON.stringify(result.data));
                          // this.user =result.data;
                          localStorage.removeItem("user");
                          localStorage.setItem("user",JSON.stringify(result.data));
                          this.user =JSON.parse(localStorage.getItem("user"))
                          // let v=JSON.stringify(this.user);
                          // console.log(this.user)
                          // axios.post('https://wenku.qikan.com:11000/admin/lib/removeTokenByUserId.html',qs.stringify({'key':result.data.unique_code}))
                          //   .then(function(res){
                          //
                          //     console.log(res.data.data);
                          //   })
                          // axios.post('https://wenku.qikan.com:11000/admin/lib/setTokenByUserId.html',qs.stringify({'key':result.data.unique_code,'value':v}))
                          //   .then(function(res){
                          //     console.log(res.data.data);
                          //   })
                          this.yzphone=false;
                          // this.$router.push({path: '/'});
                          let a=localStorage.getItem("failuretime");
                          if(a!=null && a!="" && a!=undefined){
                            console.log(a)

                            var timestamp = Date.parse(new Date())
                            console.log(timestamp)
                            if(timestamp<=a){
                              let k=localStorage.getItem("key");
                              console.log(k)
                              localStorage.removeItem("key");
                              localStorage.removeItem("failuretime")
                              // window.location.href="/article?collkey="+k
                              let jumpurl=localStorage.getItem("jumpurl");
                              localStorage.removeItem("jumpurl")

                              if(jumpurl=='/article'){
                                window.location.href="/article?collkey="+k
                              }else if(jumpurl=='/advancesearch'){
                                window.location.href="/advancesearch"
                              }else if(jumpurl=='/search'){
                                window.location.href="/search?key="+k
                              }else if(jumpurl=='/list/id'){
                                window.location.href="/list/id?id="+k
                              }

                            }else{
                              this.$router.push({path: '/'});
                            }
                          }else{
                            this.$router.push({path: '/'});
                          }
                        }else{
                          this.tipshow(result.message);
                        }
                      })
                      .catch((error)=>{
                        console.log(error)
                      })
                  }else{
                    this.isSetPassword=true;
                  }

                }
              }else{
                this.isSetPassword=true;

              }
            })
            .catch((error)=>{
              console.log(error)
            })




        },sendCode() {
          var mobilereg = /^1[345789]\d{9}$/;
          if (this.phone_zc == null || this.phone_zc == "" || this.phone_zc == 'undefined') {
            this.tipshow("手机号不能为空!");
            return false;
          } else if (!mobilereg.test(this.phone_zc)) {
            this.tipshow("手机格式不正确");
            return false;
          }
          axios.post('https://wenku.qikan.com:11000/admin/lib/sendmsg.html',qs.stringify({'mobile':this.phone_zc})
          )
            .then((response)=>{
              console.log(response.data);
              let result= response.data;
              if(result.code==1)
              {
                // this.tipshow("验证码发送成功");

                this.sendAuthCode = false;

                this.auth_time = 60;
                var auth_timetimer =  setInterval(()=>{
                  this.auth_time--;
                  if(this.auth_time<=0){
                    this.sendAuthCode = true;
                    clearInterval(auth_timetimer);
                  }
                }, 1000);
                this.start1 = true;
              }
            })
            .catch((error)=>{
              console.log(error)
            })

        }
      }
    }
</script>

<style scoped>
  .tipinfo{position: fixed; z-index: 9999; top: 50%; left: 50%; width: 400px;  height: 40px;border-radius: 10px; margin-left: -200px; margin-top: -20px; line-height: 40px;text-align: center;color: #fff; font-size: 14px; background-color: rgba(50,50,50,0.8)}

  /*.mask{ width: 100%; height: 100%; background: rgba(24,24,24,1); position: fixed;z-index: 999; left: 0; top: 0; }*/
  .mask{ width: 100%; height: 100%;  background:#F6F6F6; position: fixed;z-index: 999; left: 0; top: 0; }


  .box{ position: fixed; z-index:1000; width: 400px; height: 400px; left: calc( 50% - 200px ); top : calc( 50% - 200px ); ;text-align:center; background-color: #fff; padding:10px 35px 0;}
  .inputbox{ height: 67px; border-bottom: 1px solid #ebebeb; padding: 24px 0 10px; width: 100%; position: relative; }
  .inputbox .inputtext{ width: 100%; line-height: 32px; height: 32px; outline: none; border: none; color: #8590A6; text-align: left;font-size: 16px; }
  .inputbox .yanzheng{position: absolute;z-index: 1001; top:26px; right:0;color: #175199;font-size: 16px;}
  .wkbtn{background: #0084FF;border-radius: 3px; width: 220px; height: 36px; line-height: 36px; color:#fff;font-size: 14px;margin:50px auto 0;}
  .wenku{font-size: 24px;color: #1a1a1a; height: 33px; margin-top:46px; clear: both;  position: relative;}
  .wenku:after{content: '';position: absolute;top: -15px; right: -15px; width: 32px; height: 32px; background: url('../assets/images/icon-close.png') center center no-repeat; background-size: 14px 14px; }
  .gap70{ width: 100%; height: 40px; clear: both; }
  .wenkudec{font-size: 14px;color: #8590A6;line-height: 20px;clear: both; text-align: center;margin-top:10px;}
  .forgetbox{ width: 380px; height: auto; overflow: hidden; position: fixed; z-index:1000; left: calc( 50% - 190px ); top: calc( 50% - 200px ); background-color: #fff; padding: 30px 40px 24px 36px;}
  .forgetbox .close{ position: absolute;z-index: 1001; width: 32px; height: 32px; background: url('../assets/images/icon-close.png') center center no-repeat; background-size: 14px 14px; top:0; right:0;}
  .ffnextbtn{width: 100%; height: 40px; line-height: 40px; background-color: #0084FF;border-radius: 3px;font-size: 14px; color: #fff; text-align: center;margin-top:60px;}
  .fftitle{ width: 100%; line-height: 30px; font-size: 16px; color: #1a1a1a; font-weight: 600; }
  .ffdec{width: 100%; line-height: 18px; font-size: 12px; color: #8590A6;}
  .ffbox{ height: 60px; border-bottom: 1px solid #ebebeb; padding: 24px 0 10px; width: 100%; position: relative; }
  .ffbox .ffinputtext{ width: 100%; line-height: 28px; height: 28px; outline: none; border: none; color: #8590A6; text-align: left;font-size: 16px; }
  .ffbox .ffyanzheng{position: absolute;z-index: 1001; top:23px; right:0;color: #175199; font-size: 16px;}
  .ffok{border: 1px solid #EBEBEB;border-radius: 3px;height: 40px; line-height: 40px; width: 100%;font-size: 16px;color: #1a1a1a; text-align: center;}
  .ffok img{ width: 20px; height: 20px; vertical-align: middle; margin-right: 10px; display: inline-block; }
</style>
